using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
using CKY.AgentPlatform.Agents.Dto;

namespace CKY.AgentPlatform.Agents
{
    /// <summary>
    /// Agent管理应用服务
    /// </summary>
    public class AgentAppService : ApplicationService, IAgentAppService
    {
        private readonly IRepository<Agents.Agent, Guid> _agentRepository;

        public AgentAppService(IRepository<Agents.Agent, Guid> agentRepository)
        {
            _agentRepository = agentRepository;
        }

        /// <summary>
        /// 将 Agent 实体转换为 DTO
        /// </summary>
        private AgentDto MapToDto(Agents.Agent agent)
        {
            return new AgentDto
            {
                Id = agent.Id,
                Name = agent.Name,
                Description = agent.Description,
                Type = agent.AgentType.ToString(),
                Status = agent.Status.ToString(),
                Configuration = agent.Configuration,
                CreationTime = agent.CreationTime,
                LastModificationTime = agent.LastModificationTime
            };
        }

        /// <summary>
        /// 获取Agent分页列表
        /// </summary>
        /// <param name="input">查询参数</param>
        /// <returns>分页结果</returns>
        public async Task<PagedResultDto<AgentDto>> GetListAsync(GetAgentListInput input)
        {
            var query = await _agentRepository.GetQueryableAsync();

            // 应用过滤条件
            if (!string.IsNullOrWhiteSpace(input.Filter))
            {
                query = query.Where(a => a.Name.Contains(input.Filter) ||
                                       a.Description.Contains(input.Filter));
            }

            // 获取总数
            var totalCount = await AsyncExecuter.CountAsync(query);

            // 分页查询
            var agents = await AsyncExecuter.ToListAsync(
                query.PageBy(input.SkipCount, input.MaxResultCount)
            );

            // 转换为DTO
            var agentDtos = agents.Select(MapToDto).ToList();

            return new PagedResultDto<AgentDto>(totalCount, agentDtos);
        }

        /// <summary>
        /// 根据ID获取Agent
        /// </summary>
        /// <param name="id">Agent ID</param>
        /// <returns>Agent详细信息</returns>
        public async Task<AgentDto> GetAsync(Guid id)
        {
            var agent = await _agentRepository.GetAsync(id);
            return MapToDto(agent);
        }

        /// <summary>
        /// 创建新的Agent
        /// </summary>
        /// <param name="input">创建参数</param>
        /// <returns>创建的Agent</returns>
        public async Task<AgentDto> CreateAsync(CreateAgentInput input)
        {
            var agent = new Agents.Agent(
                GuidGenerator.Create(),
                input.Name,
                input.Description,
                input.SystemPrompt,
                input.ModelConfigId);

            await _agentRepository.InsertAsync(agent);
            return MapToDto(agent);
        }

        /// <summary>
        /// 更新Agent
        /// </summary>
        /// <param name="id">Agent ID</param>
        /// <param name="input">更新参数</param>
        /// <returns>更新后的Agent</returns>
        public async Task<AgentDto> UpdateAsync(Guid id, UpdateAgentInput input)
        {
            var agent = await _agentRepository.GetAsync(id);

            agent.Name = input.Name ?? agent.Name;
            agent.Description = input.Description ?? agent.Description;
            agent.SystemPrompt = input.SystemPrompt ?? agent.SystemPrompt;
            agent.Configuration = input.Configuration ?? agent.Configuration;

            await _agentRepository.UpdateAsync(agent);
            return MapToDto(agent);
        }

        /// <summary>
        /// 删除Agent
        /// </summary>
        /// <param name="id">Agent ID</param>
        public async Task DeleteAsync(Guid id)
        {
            await _agentRepository.DeleteAsync(id);
        }
    }
}